﻿using System;
using System.Collections.Generic;
using Business.Entities.Actions;
using Business.Entities;

namespace Business.Logic
{
    internal class RequestBuilder
    {
        private long client_version = 12653266;

        internal GoogleAction CreateTask(TaskList parentList, Task task)
        {
            if (parentList == null || String.IsNullOrEmpty(parentList.GoogleId))
                throw new ArgumentException("ParentList list not specified");
            
            var action = new GoogleAction();
            
            action.action_type = "create";
            action.index = 0;
            action.entity_delta = new EntityDelta();
            action.entity_delta.name = task.name;
            action.entity_delta.entity_type = "TASK";
            action.dest_parent_type = "GROUP";
            action.entity_delta.notes = task.notes;
            action.entity_delta.task_date = task.task_date;
            action.list_id = parentList.GoogleId;  // Always the parent list
            action.parent_id = parentList.GoogleId;// Might be another task if indented?

            /// TEST
            //action.entity_delta.metadata = new Dictionary<string, object>();
            //action.entity_delta.metadata.Add("KjellBjarne", "17");
            //action.entity_delta.list_metadata = new Dictionary<string, object>();
            //action.entity_delta.list_metadata.Add("Torgeir", "12");
            
            if (parentList.child_entity != null && parentList.child_entity.Count > 0)
            {
                var lastElem = parentList.child_entity[parentList.child_entity.Count - 1];
                action.prior_sibling_id = lastElem.GoogleId;
                action.index = parentList.GetMaxIndex() + 1;
            }
            
            return action;
        }

        internal GoogleAction MoveTask(Task task, string sourceListId, string targetListId, string targetParentId)
        {
            var action = new GoogleAction();

            action.action_type = "move";
            action.id = task.GoogleId;
            action.source_list = sourceListId;
            action.dest_list = targetListId;
            action.dest_parent = targetParentId;
            return action;
        }

        /// <summary>
        /// Set priortask to null in order to move to the top
        /// </summary>
        /// <param name="task"></param>
        /// <param name="priorTask">(Optional) desitination prior sibling</param>
        /// <param name="destParent">(Optional) destination parent</param>
        /// <returns></returns>
        internal GoogleAction MoveTaskAfter(Task task, Task priorTask, string destParent)
        {
            var action = new GoogleAction();
            string listId = task.list_id[0];
            if (listId == null) throw new ArgumentException("ListId null when attempting internal list move");
            action.action_type = "move";
            action.id = task.GoogleId;
            if (priorTask != null) action.prior_sibling_id = priorTask.GoogleId;
            action.source_list = listId;
            if (destParent == null) destParent = listId;
            action.dest_parent = destParent;
            return action;
        }

        internal GoogleAction UpdateTask(Task task)
        {
            var action = new GoogleAction();
            action.action_type = "update";
            action.id = task.GoogleId;
            action.entity_delta = new EntityDelta();
            action.entity_delta.name = task.name;
            action.entity_delta.notes = task.notes;
            action.entity_delta.task_date = task.task_date;
            action.entity_delta.completed = task.completed;
            return action;
        }

        internal GoogleAction DeleteTask(string id)
        {
            var action = new GoogleAction();
            action.action_type = "update";
            action.id = id;
            action.entity_delta = new EntityDelta();
            action.entity_delta.deleted = true;
            return action;
        }

        internal GoogleAction DeleteList(string id)
        {
            var action = new GoogleAction();
            action.IsDeleteListAction = true;
            action.action_type = "update";
            action.id = id;
            action.entity_delta = new EntityDelta();
            action.entity_delta.deleted = true;
            return action;
        }

        internal GoogleAction RenameList(string id, string newName)
        {
            var action = new GoogleAction();
            action.action_type = "update";
            action.id = id;
            action.current_list_id = id;
            action.entity_delta = new EntityDelta();
            action.entity_delta.name = newName;
            return action;
        }

        internal GoogleAction UpdateList(TaskList list)
        {
            var action = new GoogleAction();
            action.action_type = "update";
            action.id = list.GoogleId;
            action.current_list_id = list.GoogleId;
            action.entity_delta = new EntityDelta();
            action.entity_delta.name = list.name;
            action.entity_delta.list_metadata = list.list_metadata;
            return action;
        }

        internal GoogleAction CreateList(string name)
        {
            var action = new GoogleAction();
            action.action_type = "create";
            action.entity_delta = new EntityDelta();
            action.entity_delta.name = name;
            action.entity_delta.entity_type = "GROUP";
            // Må sette ? creator_id = null
            return action;
        }

        /// <summary>
        /// Creates a request to get the specified list
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal GoogleAction GetList(string id)
        {
            var action = new GoogleAction();
            action.action_type = "get_all";
            action.get_deleted = false;
            action.list_id = id;
            return action;
        }

        /// <summary>
        /// Returns an empty request shell
        /// </summary>
        /// <returns></returns>
        internal GoogleRequest CreateRequest()
        {
            var request = new GoogleRequest();
            request.client_version = client_version;
            request.action_list = new List<GoogleAction>();
            request.latest_sync_point = GoogleApi.LatestSyncPoint;
            return request;
        }

    }
}
